<?php

class Engine_Controller_Action extends Zend_Controller_Action {
	
	protected $session;
	protected $auth;
	protected $log;
	protected $cache;
	protected $translate;
	protected $locale;
	protected $acl;
	protected $_error;
	protected $page_data;
	
	public function init() {		
		// Set default view variables
		$this->_error = array();
		
		// Initialize class variables
		$this->session = new Zend_Session_Namespace();
		$this->auth = Zend_Registry::get('WJAuth');
		$this->log = Zend_Registry::get('Zend_Log');
		$this->cache = Zend_Registry::get('Zend_Cache');	
		$this->translate = Zend_Registry::get('Zend_Translate');
		$this->locale = Zend_Registry::get('Zend_Locale');	
		$this->acl = Zend_Registry::get('Zend_Acl');
		
		$this->initPage();
	}

	
	protected function initPage() {
		// Clean URI for query
		$uri = $this->view->url();			
		$uri = str_replace('index.php','',$uri);
		$uri = str_replace('//','/',$uri);
		
		$caheName = 'core_pages'.str_replace('/', '_', $uri);
		
		if (($this->page_data = $this->cache->load($caheName)) === false ) {
			$core_pages = new Engine_Db_Table_CorePages();
			$select = $core_pages->select();
			$select->where("page_uri = '$uri' AND page_published = TRUE");
			$this->page_data = $core_pages->fetchRow($select);
			$this->cache->save($this->page_data, $caheName);
		}
		
		$this->initHeadMeta($this->page_data);
		$this->updatePageData($this->page_data);
		
	}

	protected function throwError($code=404) {
		// Clear current response and set Http Code to 404
		$this->getResponse()->clearBody();
		$this->getResponse()->clearHeaders();
		throw new Zend_Controller_Action_Exception('', $code);        
	}	
	
	
	protected function initHeadMeta(&$results) {
		$this->view->title = SITE_NAME;
		
		if ($results) {
			$core_config = Zend_Registry::get('Core_Config');
			if ($core_config['site_name_in_title']) {
				$this->view->title = $this->view->title . " - " . $results['page_title'];
			} else {
				$this->view->title = $results['page_title'];
			}
			$this->view->description = $results['page_description'];
	
			$this->view->headMeta()->setName('description', $results['page_description']);
			$this->view->headMeta()->setName('keywords', $results['page_keywords']);
			
			if (($results["page_noindex"] == true) && ($results["page_nofollow"] == true)) {
				$this->view->headMeta()->setName('robots','noindex, nofollow');
			} elseif ($results["page_noindex"] == true) {
				$this->view->headMeta()->setName('robots','noindex');
			} elseif ($results["page_nofollow"] == true) {
				$this->view->headMeta()->setName('robots','nofollow');  
			}
		} 
		
		$version = new Engine_Version();
		$this->view->headMeta()->setName('generator', ('webJestic CMS '.$version->getVersion()));
		$this->view->headMeta()->appendHttpEquiv('pragma','no-cache');
		$this->view->headMeta()->appendHttpEquiv('Cache-control', 'no-cache');
		$this->view->headMeta()->appendHttpEquiv('Content-Type', 'text/html; charset=UTF-8');
	}
	
	
	protected function updatePageData(&$results) {
		// If this is not a POST then update count
		if (!$this->getRequest()->isPost()) {		
			if ($results) {
				
				$core_pages = new Engine_Db_Table_CorePages();
				//$core_pages->updateViewCount($this->page_data['page_id']);
				$select = $core_pages->select()
									->from(array('cp' => 'core_pages'), 
											array('page_views'))
									->where('page_id = ?', $results['page_id']);
				$row = $core_pages->fetchRow($select);
				
				$data = array ('page_views' => ($row['page_views']+1));
				$where = $core_pages->getAdapter()->quoteInto('page_id = ?', $results['page_id']);
				$core_pages->update($data, $where);
			}
			
		}
	}
	
}

?>